package resp

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

// Response 统一返回结构体
type Response struct {
	Code int    `json:"errorCode"`
	Data any    `json:"data"`
	Msg  string `json:"message"`
	Suc  bool   `json:"success"`
}

const (
	ERROR   = 7
	SUCCESS = 0
)

func Result(code int, data any, msg string, suc bool, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
		suc,
	})
	c.Abort()
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]any{}, "操作成功", true, c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]any{}, message, true, c)
}

func OkWithData(data any, c *gin.Context) {
	Result(SUCCESS, data, "成功", true, c)
}

func OkWithDetailed(data any, message string, c *gin.Context) {
	Result(SUCCESS, data, message, true, c)
}

func Fail(c *gin.Context) {
	Result(ERROR, map[string]any{}, "操作失败", false, c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]any{}, message, false, c)
}

func NoAuth(message string, c *gin.Context) {
	c.JSON(http.StatusUnauthorized, Response{
		7,
		nil,
		message,
		false,
	})
	c.Abort()
}

func FailWithDetailed(data any, message string, c *gin.Context) {
	Result(ERROR, data, message, false, c)
}
